import numpy as np
import os

def select_random_rows(input_file, output_file, num_rows=100000):
    # Load the data using memory mapping
    data = np.load(input_file, mmap_mode='r')
    
    # Get the total number of rows
    total_rows = data.shape[0]
    
    # Randomly select unique indices
    random_indices = np.random.choice(total_rows, size=min(num_rows, total_rows), replace=False)
    
    # Select the corresponding rows
    selected_data = data[random_indices]
    
    # Save the selected data to a new file
    np.save(output_file, selected_data)
    print(f"Saved selected data to {output_file}")

if __name__ == "__main__":
    # Path to the input .npy file
    input_file_path = "/mnt/d/u_XJTU.Dec/working_dir/1d_flame_60nh3/1Dflame_.npy"  # Change this to your file path
    
    # Create output file path with _selected_10w suffix
    base_name, ext = os.path.splitext(input_file_path)
    output_file_path = f"{base_name}_selected_10w{ext}"
    
    # Call the function to select random rows and save them
    select_random_rows(input_file_path, output_file_path)